/**
 * 
 */
package com.tony.firewall.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.view.ViewGroup;

/**
 * @author lbingt
 * 
 */
public class FragmentPageAdapter extends FragmentPagerAdapter {
	private ArrayList<Fragment> mFragments = new ArrayList<Fragment>(3);
	private ArrayList<String> mTitles = new ArrayList<String>(3);
	private Context mContext;
	private String[] titles;

	private final FragmentManager mFragmentManager;
	private FragmentTransaction mCurTransaction;

	public FragmentPageAdapter(FragmentManager fm) {
		super(fm);
		mFragmentManager = fm;
		mCurTransaction = fm.beginTransaction();
	}

	public FragmentPageAdapter(FragmentManager fm, Context ctx) {
		this(fm);
		mContext = ctx;
	}

	public FragmentPageAdapter(FragmentManager fm, ArrayList<Fragment> fragments, String[] title) {
		this(fm);
		mFragments = fragments;
		titles = title;
	}

	public void addItem(int index, String title, Fragment fg) {
		mTitles.add(index, title);
		mFragments.add(index, fg);
	}

	public void addItem(int tag, Fragment fg) {
		// if (mFragments.size() < 1 || mFragments.get(tag) == null) {
		mFragments.add(tag, fg);
		// }
	}

	public void setTitle(String[] title) {
		titles = title;
	}

	@Override
	public CharSequence getPageTitle(int position) {
		// return mTitles.get(position);
		return titles[position];
	}

	@Override
	public Fragment getItem(int arg0) {
//		return new EntityFragment();
		Fragment fragment = mFragments.get(arg0);
		return fragment;
//		 return mFragments.get(arg0);
	}

	@Override
	public int getCount() {
		return mFragments.size();
	}

	@Override
	public int getItemPosition(Object object) {
		// TODO Auto-generated method stub
		return POSITION_NONE;
	}

//	@Override
//	public boolean isViewFromObject(View view, Object obj) {
//		// TODO Auto-generated method stub
//		return view == obj;
//	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		 super.destroyItem(container, position, object);
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		 return super.instantiateItem(container, position);
	}

}
